package top.xunfong.probe.service;

import cn.hutool.json.JSONUtil;
import top.xunfong.api.probe.constant.ProbeConstant;
import top.xunfong.api.probe.domain.dto.ProbeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class KafkaConsumerService {

    @Autowired
    private ProbeService probeService;

    @KafkaListener(topics = {ProbeConstant.TOPIC}, groupId = ProbeConstant.GROUP_ID)
    void probeConsumer(List<String> messages) {
        List<ProbeDTO> probeList = messages.stream().map(message -> JSONUtil.toBean(message, ProbeDTO.class)).toList();
        probeService.batchSaveProbe(probeList);
    }
}
